package com.qianfeng.es.service;

//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼                  BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  
//  


import com.qianfeng.es.utils.ConnectionUtil;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

/**
 * Created by Jackiechan on 2022/1/17/15:01
 *
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */
public class DocServiceImpl implements DocService{
    @Override
    public void addDoc(String index,String source) throws IOException {
        RestHighLevelClient highLevelClient = ConnectionUtil.getClient();
        IndexRequest indexRequest = new IndexRequest(index);
        indexRequest.source(source, XContentType.JSON);//设置要添加的数据以及格式为json
        IndexResponse response = highLevelClient.index(indexRequest, RequestOptions.DEFAULT);
        System.err.println(response.toString());
    }

    @Override
    public void addDocWithId(String index, String id, String source) throws IOException {
        RestHighLevelClient highLevelClient = ConnectionUtil.getClient();
        IndexRequest indexRequest = new IndexRequest(index);
        indexRequest.id(id);//手动设置id
        indexRequest.source(source, XContentType.JSON);//设置要添加的数据以及格式为json
        IndexResponse response = highLevelClient.index(indexRequest, RequestOptions.DEFAULT);
        System.err.println(response.toString());
    }

    @Override
    public void updateDoc(String index,String id, String source) throws Exception {
        RestHighLevelClient highLevelClient = ConnectionUtil.getClient();
        UpdateRequest updateRequest=new UpdateRequest(index,id);//设置要更新的库和id
        updateRequest.doc(source, XContentType.JSON);//设置要更新的内容
        UpdateResponse response = highLevelClient.update(updateRequest, RequestOptions.DEFAULT);
        System.err.println(response.toString());
    }

    @Override
    public void deleteDoc(String index, String id) throws Exception {
        RestHighLevelClient highLevelClient = ConnectionUtil.getClient();
        DeleteRequest deleteRequest = new DeleteRequest(index, id);
        DeleteResponse response = highLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
        System.err.println(response.toString());
    }
}
